package com.example.administrator.hqq.fragment;


import android.annotation.SuppressLint;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.hqq.R;
import com.example.administrator.hqq.adapter.FragmentsAdapter;
import com.example.administrator.hqq.base.MyFragemnt;
import com.example.administrator.hqq.bean.event.InformationEvent;
import com.example.administrator.hqq.config.LoongsConfig;
import com.example.administrator.hqq.fragment.information.MessageFragment;
import com.example.administrator.hqq.fragment.information.NotificationFragment;
import com.zhy.autolayout.AutoRelativeLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 消息fragment
 */
public class InformationFragment extends MyFragemnt {


    Unbinder unbinder;
    @BindView(R.id.information_number)
    TextView informationNumber;
    @BindView(R.id.information)
    AutoRelativeLayout information;
    @BindView(R.id.post_view)
    View postView;
    @BindView(R.id.normal_auto)
    AutoRelativeLayout normalAuto;
    @BindView(R.id.notification_number)
    TextView notificationNumber;
    @BindView(R.id.notification)
    AutoRelativeLayout notification;
    @BindView(R.id.discover_view)
    View discoverView;
    @BindView(R.id.notification_auto)
    AutoRelativeLayout notificationAuto;
    @BindView(R.id.information_vp)
    ViewPager informationVp;
    private InformationNumber setInformationNumber;
    private View inflate;
    private Point point = new Point();
    private List<Fragment>fragments;
    private Boolean isShow;
    private MineFragment.AddModule addModule;

    public void setAddModule(MineFragment.AddModule addModule) {
        this.addModule = addModule;
    }

    public InformationFragment() {
        // Required empty public constructor
    }

    public void setInformationNumber(InformationNumber setInformationNumber) {
        this.setInformationNumber = setInformationNumber;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        inflate = inflater.inflate(R.layout.fragment_information, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        EventBus.getDefault().register(this);
        initview();
        return inflate;
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initview() {
        fragments = new ArrayList<>();
        MessageFragment messageFragment = new MessageFragment();
        NotificationFragment notificationFragment = new NotificationFragment();
        fragments.add(messageFragment);
        fragments.add(notificationFragment);
        FragmentsAdapter fragmentsAdapter = new FragmentsAdapter(getChildFragmentManager(), fragments);
        informationVp.setAdapter(fragmentsAdapter);
        normalAuto.setSelected(true);
        notificationFragment.setAddModule(new MineFragment.AddModule() {
            @Override
            public void addModule(String category_id) {
                if (addModule!=null){
                    addModule.addModule(category_id);
                }
            }
        });
        informationVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    normalAuto.setSelected(true);
                    notificationAuto.setSelected(false);
                } else {
                    notificationAuto.setSelected(true);
                    normalAuto.setSelected(false);
                }
                changeCount();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //当APP第一次进入是显示之前记录的消息模块每个页面的计数
       setNumber();
    }
    public void changeCount(){
        //当切换消息模块的时候判断切换的那个页面来更新对应的计数显示
        int notification_number = LoongsConfig.getUsersIdSPUtils().getInt(LoongsConfig.MessageNotification.InformationNotificationNumber, 0);
        int messageNumber = LoongsConfig.getUsersIdSPUtils().getInt(LoongsConfig.MessageNotification.InformationMessageNumber, 0);
        boolean normalAuto = getNormalAuto();
        if (normalAuto){
            messageNumber=0;
            LoongsConfig.getUsersIdSPUtils().put(LoongsConfig.MessageNotification.InformationMessageNumber,messageNumber);
        }else{
            notification_number=0;
            LoongsConfig.getUsersIdSPUtils().put(LoongsConfig.MessageNotification.InformationNotificationNumber,notification_number);
        }
        //更新首页消息模块的计数
        if (setInformationNumber!=null)
        {
            setInformationNumber.setMainInformationNumber();
        }
        setNumber();
    }
    /**
     * 根据SP缓存的数据来判读消息和通知的显示数量
     */
    private void setNumber() {
        int notification_number = LoongsConfig.getUsersIdSPUtils().getInt(LoongsConfig.MessageNotification.InformationNotificationNumber, 0);
        int messageNumber = LoongsConfig.getUsersIdSPUtils().getInt(LoongsConfig.MessageNotification.InformationMessageNumber, 0);
        //当数量小于0时候隐藏 大于0小于等于99时候正常显示 大于99时省略号显示
        Log.e("kang", "setNumber: "+notification_number+"     "+messageNumber);
        if (notification_number<=0){
            notificationNumber.setVisibility(View.GONE);
        }else if (notification_number>0&&notification_number<=99){
            notificationNumber.setVisibility(View.VISIBLE);
            notificationNumber.setText(notification_number+"");
        }else{
            notificationNumber.setVisibility(View.VISIBLE);
            notificationNumber.setText("…");
        }
        if (messageNumber<=0){
            informationNumber.setVisibility(View.GONE);
        }else if (messageNumber>0&&messageNumber<=99){
            informationNumber.setVisibility(View.VISIBLE);
            informationNumber.setText(messageNumber+"");
        }else{
            informationNumber.setVisibility(View.VISIBLE);
            informationNumber.setText("…");
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(InformationEvent informationEvent)
    {
        int notification_number = LoongsConfig.getUsersIdSPUtils().getInt(LoongsConfig.MessageNotification.InformationNotificationNumber, 0);
        int messageNumber = LoongsConfig.getUsersIdSPUtils().getInt(LoongsConfig.MessageNotification.InformationMessageNumber, 0);
        //判读是否在消息页面
        Log.e("kang", "Event: "+isShow );
        if (isShow){
            boolean normalAuto = getNormalAuto();
            //判断在消息页面还是通知页面 当在某一个页面是 此页面的消息不进行计数处理
            if (normalAuto){
                messageNumber=0;
                LoongsConfig.getUsersIdSPUtils().put(LoongsConfig.MessageNotification.InformationMessageNumber,messageNumber);
            }else{
                notification_number=0;
                LoongsConfig.getUsersIdSPUtils().put(LoongsConfig.MessageNotification.InformationNotificationNumber,notification_number);
            }
        }
        setNumber();
        //更新首页消息模块的计数
        if (setInformationNumber!=null)
        {
            setInformationNumber.setMainInformationNumber();
        }

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    /**
     * 根据isVisibleToUser来判读是否进入消息模块
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        isShow=isVisibleToUser;
        if (isVisibleToUser)
        {
            //当点击进入消息模块时 判读是显示的是消息页面还是通知页面
            if (getNormalAuto())
            {
                LoongsConfig.getUsersIdSPUtils().put(LoongsConfig.MessageNotification.InformationMessageNumber,0);
            }else
            {
                LoongsConfig.getUsersIdSPUtils().put(LoongsConfig.MessageNotification.InformationNotificationNumber,0);
            }
            setNumber();
        }else{
        }
        Log.e("kang", "setUserVisibleHint: "+isShow );
    }

    public boolean getNormalAuto() {
        if (normalAuto!=null)
        return normalAuto.isSelected();
        return false;
    }

    public boolean getNotificationAuto() {
        return notificationAuto.isSelected();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }
    public  interface InformationNumber{
        void setMainInformationNumber();
    }

    @OnClick({R.id.normal_auto, R.id.notification_auto})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.normal_auto:
                informationVp.setCurrentItem(0);
                normalAuto.setSelected(true);
                notificationAuto.setSelected(false);
                changeCount();
                break;
            case R.id.notification_auto:
                informationVp.setCurrentItem(1);
                notificationAuto.setSelected(true);
                normalAuto.setSelected(false);
                changeCount();
                break;
        }
    }
}
